# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html


import logging
import os
import pymongo

from scrapy import Request
from scrapy.pipelines.images import ImagesPipeline

from .settings import IMAGES_STORE


class DouyuImagesPipeline(ImagesPipeline):
    """
    通过scrapy自带模块的ImagesPipeline类,将图片存储在IMAGES_STORE中.这里重写get_media_requests()方法构建请求返回给引擎
    ImagesPipeline类所在地址: /home/python/.virtualenvs/spider_py2/lib/python2.7/site-packages/scrapy/pipelines/images.py
    重写item_completed()方法来更改图片保存路径
    ps : 如果有重复图片、文件，保存到本地只有一份，后续改名只能成功一次，后面再改名
         用商品名称做为图片名保存，如果图片名里有"/"，则保存时会当作路径结点使用, 因此需替换, 同时注意类似的转义
    """
    def get_media_requests(self, item, info):
        # get_media_requests的作用就是为每一个图片链接生成一个Request对象，这个方法的输出将作为item_completed的输入中的results，results是一个元组，每个元组包括(success, imageinfoorfailure)。如果success=true，imageinfoor_failure是一个字典，包括url/path/checksum三个key
        yield Request(item['image_url'])
        #return [Request(item["image_url"])]  # 此时这个和yield一样

    def item_completed(self, results, item, info):
        """重命名图片保存地址"""
        # 固定写法，获取图片路径，同时判断这个路径是否正确，如果正确，就放到 image_path里，ImagesPipeline源码剖析可见, 需注意源码中path是相对路径, 重写后需补齐路径
        print("--" * 30)
        print(results)
        print('--' * 30)

        """
        [x for ok, x in results if ok]

        results = [
            (
                True,
                {
                    'url': 'https://rpic.douyucdn.cn/live-cover/roomCover/2018/10/21/5547decb995fee7fba3ac77f37546a89_small.jpg',
                    'path': 'full/4122a8e6a6cb9802da4d60f775528bd85b581f5f.jpg',
                    'checksum': '86d16540938cbd1fa970985318207df2'
                }
            )
        ]
        """

        old_name = IMAGES_STORE + [x['path'] for ok, x in results if ok][0]
        new_name = IMAGES_STORE + item['nick_name'] + ".jpg"

        logging.info("Hello World!")

        try:
            os.rename(old_name, new_name)
            item["image_path"] = new_name
        except:
            logging.error("<{}> download failed".format(item['image_url']))

        return item


class DouyuMongoPipeline(object):
    """通用存储"""
    def open_spider(self, spider):
        self.conn = pymongo.MongoClient().douyu.item

    def process_item(self, item, spider):
        spider.logger.info("Hello World")
        self.conn.insert(dict(item))
        return item
